package com.jxinternet.platform.services.service.impl.steps;

import com.jxinternet.platform.services.entity.domian.Log;
import com.jxinternet.platform.services.entity.domian.Message;
import com.jxinternet.platform.services.entity.domian.User;
import com.jxinternet.platform.services.entity.domian.Wealth;
import com.jxinternet.platform.services.entity.protobuf.RegisterProtos.RegisterRequest;
import com.jxinternet.platform.services.protocol.BaseProtocol;
import com.jxinternet.platform.services.protocol.Constants;
import com.jxinternet.platform.services.repository.convert.LogConvert;
import com.jxinternet.platform.services.repository.convert.UserConvert;
import com.jxinternet.platform.services.repository.convert.WealthContert;
import com.jxinternet.platform.services.repository.mysql.LogRepository;
import com.jxinternet.platform.services.repository.mysql.UserRepository;
import com.jxinternet.platform.services.repository.mysql.WealthRepository;
import com.jxinternet.platform.services.util.MD5Util;
import com.jxinternet.platform.services.util.RandomUtil;
import com.jxinternet.platform.services.util.SecurityUtil;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by gold-jx on 2017/6/26.
 */
@Slf4j
@Component
public class RegisterSteps {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private WealthRepository wealthRepository;

    @Autowired
    private LogRepository logRepository;

    @Autowired
    private UserConvert userConvert;

    @Autowired
    private WealthContert wealthContert;

    @Autowired
    private LogConvert logConvert;


    /**
     * step1
     * 验证请求
     */
    public Message step1_validateRequest(RegisterRequest registerRequest) {
        Message message = new Message();
        try {
            //1.请求不能为空
            if (registerRequest == null) {
                message.setCode(BaseProtocol.User_Invalid_Request_Code);
                message.setRes(BaseProtocol.User_Invalid_Request_Des);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            //2.用户名不能为空
            if ("".equals(registerRequest.getUserName()) || registerRequest.getUserName() == null) {
                message.setCode(BaseProtocol.User_Invalid_UserName_NotNull_Code);
                message.setRes(BaseProtocol.User_Invalid_UserName_NotNull_Des);
                //log.error(message.getRes(), message.getCode());
                return message;
            }

            //3.密码不能为空
            if ("".equals(registerRequest.getPassword()) || registerRequest.getPassword() == null) {
                message.setCode(BaseProtocol.User_Invalid_Password_NoNull_Code);
                message.setRes(BaseProtocol.User_Invalid_assword_NotNull_Des);
                //log.error(message.getRes(), message.getCode());
                return message;
            }

            //4.昵称不能为空
            if ("".equals(registerRequest.getNickName()) || registerRequest.getNickName() == null) {
                message.setCode(BaseProtocol.User_Invalid_NickName_NoNull_Code);
                message.setRes(BaseProtocol.User_Invalid_NickName_NotNull_Des);
                //log.error(message.getRes(), message.getCode());
                return message;
            }

        } catch (Exception e) {
            message.setCode(BaseProtocol.Register_Step01_Exception_Code);
            message.setRes(BaseProtocol.Register_Step01_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }
        return message;

    }

    /**
     * step2
     * 验证用户
     */
    public Message step2_validateUser(String userName) {
        Message message = new Message();
        try {
            User user =userConvert.convertMysqlDomainToModel(userRepository.findByUserName(userName));
            if (user != null) {
                message.setCode(BaseProtocol.User_Invalid_User_isExist_Code);
                message.setRes(BaseProtocol.User_Invalid_User_isExist_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }

        } catch (Exception e) {
            message.setCode(BaseProtocol.Register_Step02_Exception_Code);
            message.setRes(BaseProtocol.Register_Step02_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }

        return message;
    }

    /**
     * step3
     * 生成用户对象
     */
    public Message step3_makeUser(RegisterRequest registerRequest) {
        Message message = new Message();
        try {
            //1.盐值
            String salt = RandomUtil.generateString(16);

            //2.密码加密
            String password = MD5Util.getMD5to16(salt + registerRequest.getPassword());
            //3.增加-数据库
            User userNew = new User();
            userNew.setUserName(registerRequest.getUserName());
            userNew.setPassword(password);
            userNew.setNickName(registerRequest.getNickName());
            userNew.setSalt(salt);
            userNew.setState(Constants.STATE_NORMAL);
            userNew.setPlatformNo(Constants.PLATFORM_NO);
            userNew.setCreateTime(new Date());
            userNew.setUpdateTime(new Date());
            userNew.setVersion(Constants.VERSION_INIT);
            message.setData(userNew);
        } catch (Exception e) {
            message.setCode(BaseProtocol.Register_Step03_Exception_Code);
            message.setRes(BaseProtocol.Register_Step03_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }
        return message;


    }


    /**
     * step4
     * 添加用户
     */
    public Message step4_addUser(User user) {
        Message message = new Message();
        try {
            User user1 = userConvert.convertMysqlDomainToModel(userRepository.save(userConvert.convertMysql(user)));
            if (user1.getId() ==null) {
                message.setCode(BaseProtocol.User_Invalid_Add_User_Code);
                message.setRes(BaseProtocol.User_Invalid_Add_User_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }

        } catch (Exception e) {
            message.setCode(BaseProtocol.Register_Step04_Exception_Code);
            message.setRes(BaseProtocol.Register_Step04_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }

        return message;
    }

    /**
     * step5
     * 验证财富
     */
    public Message step5_validate_Wealth(String userName) {
        Message message = new Message();
        try {
            Wealth wealth =wealthContert.convertMysqlDomainToModel(wealthRepository.findByUserName(userName));
            if (wealth != null) {
                message.setCode(BaseProtocol.User_Invalid_Wealth_isExist_Code);
                message.setRes(BaseProtocol.User_Invalid_Wealth_isExist_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }

        } catch (Exception e) {
            message.setCode(BaseProtocol.Register_Step05_Exception_Code);
            message.setRes(BaseProtocol.Register_Step05_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }
        return message;
    }

    /**
     * step6
     * 制作财富对象
     */
    public Message step6_makeWealth(String userName) {
        Message message = new Message();
        try {
            Wealth wealth = new Wealth();
            wealth.setUserName(userName);
            wealth.setRechargePoint(Constants.WEALTH_INIT_GOLD_COUNT);
            wealth.setState(Constants.STATE_NORMAL);
            wealth.setPlatformNo(Constants.PLATFORM_NO);
            wealth.setUpdateTime(new Date());
            wealth.setCreateTime(new Date());
            wealth.setVersion(Constants.VERSION_INIT);
            message.setData(wealth);
            return message;
        } catch (Exception e) {
            message.setCode(BaseProtocol.Register_Step06_Exception_Code);
            message.setRes(BaseProtocol.Register_Step06_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }
    }


    /**
     * step7
     * 添加财富
     */
    public Message step7_createWealth(Wealth wealth) {
        Message message = new Message();
        try {
            Wealth wealth1=wealthContert.convertMysqlDomainToModel(wealthRepository.save(wealthContert.convertMysql(wealth)));
            if (wealth1.getId() == null) {
                message.setCode(BaseProtocol.User_Invalid_Wealth_Init_Code);
                message.setRes(BaseProtocol.User_Invalid_Wealth_Init_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            return message;
        } catch (Exception e) {
            message.setCode(BaseProtocol.Register_Step07_Exception_Code);
            message.setRes(BaseProtocol.Register_Step07_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }
    }


    /**
     * step8
     * 生成token
     */
    public Message step8_createToken(String userName) throws Exception {
        Message message = new Message();
        try {
            String token = SecurityUtil.authentication(userName);
            message.setData(token);
            return message;

        } catch (Exception e) {
            message.setCode(BaseProtocol.Register_Step08_Exception_Code);
            message.setRes(BaseProtocol.Register_Step08_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }

    }


    public Message step9_saveLog(String userName, String loginIp) {
        Message message = new Message();
        try {
            Log log = new Log();
            log.setUserName(userName);
            log.setLoginIp(loginIp);
            log.setType(BaseProtocol.Area_User_Register);
            log.setOperation("注册");
            log.setState(Constants.STATE_NORMAL);
            log.setPlatformNo(Constants.PLATFORM_NO);
            log.setCreateTime(new Date());
            log.setUpdateTime(new Date());
            log.setVersion(Constants.VERSION_INIT);
            logRepository.save(logConvert.convertMysql(log));
            return message;

        } catch (Exception e) {
            message.setCode(BaseProtocol.Register_Step09_Exception_Code);
            message.setRes(BaseProtocol.Register_Step09_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;

        }


    }


}
